package com.wanxi.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author Li
 * @Date 2023/5/3 21:38
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "用户",description = "用于描述用户对象")
@TableName("user")
public class UserModel implements UserDetails {
    @TableId(value = "id", type = IdType.ASSIGN_ID)//雪花算法
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "用户ID",example = "12")
    private Long id;
    @ApiModelProperty(value = "用户姓名",example = "张三")
    private String username;
    @ApiModelProperty(value = "用户密码",example = "123XXX")
    private String password;
    @ApiModelProperty(value = "用户年龄",example = "12")
    private Integer age;
    @ApiModelProperty(value = "用户性别",example = "男")
    private String sex;
    @ApiModelProperty(value = "用户电话",example = "13686988868")
    private String phone;
    @ApiModelProperty(value = "用户邮箱",example = "123@qq.com")
    private String email;
    @ApiModelProperty(value = "用户地址",example = "四川成都")
    private String address;
    @ApiModelProperty(value = "用户状态",example = "1")
    private Integer enable;
    @ApiModelProperty(value = "备注",example = "该用户为管理员")
    private String remark;
    @ApiModelProperty(value = "用户创建时间",example = "2023-7-14")
    private String createTime;
    @ApiModelProperty(value = "用户修改时间",example = "2023-7-14")
    private String updateTime;

    @TableField(exist = false)
    private Integer startAge;
    @TableField(exist = false)
    private Integer endAge;

    @TableField(exist = false)

    private String code;
    @TableField(exist = false)

    private String vCode;
    @TableField(exist = false)
    private Integer page; //页数
    @TableField(exist = false)
    private Integer limit; //条数

    @TableField(exist = false)
    private List<String> permissions;
    @TableField(exist = false)
    @JSONField(serialize = false)//不需要进行序列化存入Redis中
    @JsonIgnore
    private List<GrantedAuthority> authorities;//定义为成员变量，只需要转换一次，不用重复调用getAuthorities()方法

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if(Objects.isNull(authorities)){
            authorities =new ArrayList<>();
        }
        for (String permission : permissions) {
            SimpleGrantedAuthority authority1 = new SimpleGrantedAuthority(permission);
            authorities.add(authority1);
        }
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
